﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Room
{
    public class RCAIUnionMonster : RCAIUnionFightBase
    {
        #region 数据
        public InstUFCreep instData;
        #endregion
        

        public RCAIUnionMonster(RoomCharactor owner)
            :base(owner)
        {
            this.type = AIType.UnionMonster;
        }

        public void Init(InstUFCreep instData)
        {
            InitFSM();
        }

        public void UpdateData(InstUFCreep instData)
        {
            this.instData = instData;
        }

        #region FSM
        private UnionMonsterState currState = UnionMonsterState.None;
        public UnionMonsterState CurrState
        {
            get { return currState; }
        }

        public RCAIUnionMonsterBase state;

        private Dictionary<UnionMonsterState, RCAIUnionMonsterBase> allState = new Dictionary<UnionMonsterState, RCAIUnionMonsterBase>();

        public override void Tick()
        {
            base.Tick();
            //allState[currState].Tick();
            if (state != null)
                state.Tick();
        }

        private void InitFSM()
        {
            allState[UnionMonsterState.None] = new RCAIUnionMonsterBase();
            allState[UnionMonsterState.Stand] = new RCAIUnionMonsterStand();
            allState[UnionMonsterState.Attack] = new RCAIUnionMonsterAttack();
            allState[UnionMonsterState.Death] = new RCAIUnionMonsterDeath();
			allState[UnionMonsterState.Move] = new RCAIUnionMonsterMove();

            foreach (var state in allState)
            {
                state.Value.controller = this;
            }
            SwitchState((int)UnionMonsterState.Stand);
        }

        public override void SwitchState(int newState)
        {
            if ((int)currState != newState)
            {
                allState[currState].Leave();
                currState = (UnionMonsterState)newState;
                allState[currState].Enter();
                state = allState[currState];
            }
        }

        public override void SwitchState(int newState, object param)
        {
            if ((int)currState != newState)
            {
                allState[currState].Leave();
                currState = (UnionMonsterState)newState;
                allState[currState].Enter(param);
                state = allState[currState];
            }
        }

        #endregion
    }
}